package com.qiaoyanqing.ximalaya.discovermodule.discover_adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qiaoyanqing.ximalaya.R;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.DiscoverRankSecondDataEntity;
import com.qiaoyanqing.ximalaya.util.CircleTransform;
import com.qiaoyanqing.ximalaya.util.MyLog;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by 佳男 on 2016/12/11.
 */

public class DiscoverRankSecondSingleAdapter extends BaseAdapter {
    private Context context;
    private List<DiscoverRankSecondDataEntity> list;
    private LayoutInflater inflater;
    private String contentType;
    private String url;
    private final String EMPTY_URL="http://fdfs.xmcdn.com/group25/M07/9C/73/wKgJNlhH3BaBkUXnAATGSSgr1kA757_web_meduim.jpg";
    public DiscoverRankSecondSingleAdapter(Context context, List<DiscoverRankSecondDataEntity> list, String contentType) {
        this.context = context;
        this.contentType=contentType;
        this.list = list;
        inflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final DiscoverRankSecondAdapter.ViewHolder holder;
        if(view==null){
            view=inflater.inflate(R.layout.rank_second_item,viewGroup,false);
            holder=new DiscoverRankSecondAdapter.ViewHolder(view);
            view.setTag(holder);

        }else{
            holder= (DiscoverRankSecondAdapter.ViewHolder) view.getTag();
        }
        holder.index.setText(""+(i+1));
        holder.title.setText(list.get(i).getTitle());
        holder.nickname.setText(list.get(i).getNickname());
        url=list.get(i).getCoverSmall();
        String path="http://fdfs.xmcdn.com/group25/M09/9B/32/wKgJNlhHh7Siq0q1AAHnL2U53oE623_web_meduim.jpg";
        MyLog.i("AAAAAAAAAA",url);
        if(contentType.equals("track")){
            holder.cardView.setRadius(holder.cardView.getWidth()/2);
            if(!url.isEmpty()){
                Picasso.with(context)
                        .load(url)
                        .transform(new CircleTransform())
                        .into(holder.icon);
            }else {
                Picasso.with(context)
                        .load(EMPTY_URL)
                        .transform(new CircleTransform())
                        .into(holder.icon);
            }
        }else {
            if(!url.isEmpty()){
                Picasso.with(context)
                        .load(url)
                        .into(holder.icon);
            }else {
                Picasso.with(context)
                        .load(EMPTY_URL)
                        .into(holder.icon);
            }
        }

        return view;
    }
    public static class ViewHolder{
        TextView index;
        ImageView icon;
        TextView title;
        TextView nickname;
        ImageView downloadIcon;
        CardView cardView;
        public ViewHolder(View view){
            cardView=(CardView)view.findViewById(R.id.rank_second_card);
            index=(TextView)view.findViewById(R.id.rank_second_index);
            icon=(ImageView)view.findViewById(R.id.rank_second_icon);
            title=(TextView)view.findViewById(R.id.rank_second_title);
            nickname=(TextView)view.findViewById(R.id.rank_second_nickname);
            downloadIcon=(ImageView)view.findViewById(R.id.rank_second_download_icon);
        }
    }
}
